package cn.com.lanou.wechat.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.lanou.wechat.service.WechatService;

@Controller
@RequestMapping("/wechat")
public class WechatController {
	private Logger log = Logger.getLogger(WechatController.class);
	private static final String token = "zxz";
	private static String appid = "wx539cf7e29ad2cfab";
	private static String secret="d2fbd4fd3ce84069c50898fb61278e9f";
	
	@Resource
	private WechatService wechatService;
	
	/**
	 * 微信认证url
	 * @param request
	 * @param response
	 */
	@RequestMapping("portal")
	public void portal(HttpServletRequest request,HttpServletResponse response) {
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce  = request.getParameter("nonce");
		String echostr  = request.getParameter("echostr");
		log.info("signature:" + signature);
		log.info("timestamp:" + timestamp);
		log.info("nonce:" + nonce);
		log.info("echostr:" + echostr);
		
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.append(echostr);
			pw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping("createMenu")
	public void createMenu(HttpServletRequest request,
			HttpServletResponse response) {
		String access_token = wechatService.findToken();
		String message = wechatService.creatMenu(access_token);
		log.info(message);
	}
}
